package com.wenhao.single;

/**
 * 是线程安全的，但是效率比较低
 *
 * @author Wenhao Tong
 * @create 2021-08-20 21:03
 */
public class LazySyn {

    private volatile static LazySyn instance;

    private LazySyn() {

    }

    public synchronized LazySyn getInstance() {
        if (instance == null) {
            instance = new LazySyn();
        }
        return instance;
    }

    /**
     * double check
     * 注意需要保证可见性、
     * 这个是线程安全的
     * @return 单例对象
     */
    public LazySyn getInstanceDoubleCheck() {
        if (instance == null) {
            synchronized (Lazy.class) {
                if (instance == null) {
                    instance = new LazySyn();
                }
            }
        }
        return instance;
    }

}
